package sz.com.postoffice.jxyzwgcase.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import sz.com.postoffice.jxyzwgcase.dao.node.ForecastVisitLogDao;
import sz.com.postoffice.jxyzwgcase.pojo.ForecastVisitLog;
import sz.com.postoffice.jxyzwgcase.service.ForecastIncomeService;

import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * 描述:预测结构包含（totalValue，details，statistics，rank）
 * 预计总收入，收入详细信息，投递和揽收，排名
 *
 * @author baiyu, du
 * @date 2020/11/27 15:15
 */
@Service
@Slf4j
public class ForecastIncomeServiceImpl implements ForecastIncomeService {

    @Autowired
    private ForecastVisitLogDao visitLogDao;

    /**
     * 新增用户访问记录
     *
     * @param emp_code
     * @param beginTime
     */
    @Override
    public void addAccessRecord(String emp_code, Long beginTime) {
        if (StringUtils.isEmpty(emp_code) || StringUtils.isEmpty(beginTime)) {
            throw new RuntimeException("传入数据有误");
        }
        Timestamp timestamp = new Timestamp(beginTime);
        LocalDateTime parseTIme = timestamp.toLocalDateTime();
        LocalDate date = parseTIme.toLocalDate();

        //  检查当日是否存过对象
        ForecastVisitLog searchEntity = new ForecastVisitLog(null, emp_code, date, null, null, null);
        //判断是否是当日一次访问
        ForecastVisitLog visitLog = visitLogDao.findByEntity(searchEntity);
        if (visitLog != null) {
            //不是第一次访问
            ////判断是否是同一次访问
            if (visitLog.getLastAccessTime().compareTo(timestamp)!=0 ) {
                //更新当前访问次数
                visitLog.setCount(visitLog.getCount() + 1);
                //更新最后访问时间
                visitLog.setLastAccessTime(timestamp);
            }
            //增加访问时长
            visitLog.setDurationTime(visitLog.getDurationTime() + 5);
            visitLogDao.update(visitLog);
        } else {
            //当日第一次访问
            ForecastVisitLog forecastVisitLog = new ForecastVisitLog(null, emp_code, date, timestamp, 1, 5);
            visitLogDao.add(forecastVisitLog);
        }

    }



}
